package com.example.administrator.yuejian35.Find.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.administrator.yuejian35.Find.FindModel;
import com.example.administrator.yuejian35.Find.activity.PostActivity;
import com.example.administrator.yuejian35.Find.bean.Post;
import com.example.administrator.yuejian35.Find.bean.PostPic;
import com.example.administrator.yuejian35.R;
import com.example.administrator.yuejian35.Recommend.bean.Cancleguanzhu;
import com.example.administrator.yuejian35.RoundTransform;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.example.administrator.yuejian35.Config.MasterImages_URL;
import static com.example.administrator.yuejian35.Config.PostImages_URL;

/**
 * Created by HP on 2017/9/26.
 */

public class FindAdapter1 extends RecyclerView.Adapter {
    private Context context;
    private List<Post> postlist=new ArrayList<>();
    private List<Integer> heightList;


    public FindAdapter1(Context c, List<Post> postlist){
        this.context = c;
        this.postlist.addAll(postlist);

    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //1)  将item.xml 生成 itemView  -  View
        //2)  生成MyViewHolderd对象
        View itemView = LayoutInflater.from(context).inflate(R.layout.item_postlist1,parent,false);
        PostViewHolder1 holder = new PostViewHolder1(itemView);
        return holder;
    }



    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        //用于绑定数据
        //转换类型
       final PostViewHolder1 viewHolder= (PostViewHolder1) holder;

        final Post post = postlist.get(position);

        /*final Course course1 = (Course) courseList.get(position);*/
        viewHolder.name.setText(postlist.get(position).getUname());
        viewHolder.time.setText(postlist.get(position).getDate());
        viewHolder.content.setText(postlist.get(position).getContent());
        viewHolder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FindModel findmodel = new FindModel();
                Call call = findmodel.deletepost(postlist.get(position).getPid());
                call.enqueue(new Callback<Cancleguanzhu>() {
                    @Override
                    public void onResponse(Call<Cancleguanzhu> call, Response<Cancleguanzhu> response) {
                        Toast.makeText(context,"删除成功！", Toast.LENGTH_LONG).show();
                        postlist.remove(position);
                        notifyDataSetChanged();
                    }

                    @Override
                    public void onFailure(Call<Cancleguanzhu> call, Throwable t) {

                    }
                });
            }
        });


        //图片
        FindModel findmodel = new FindModel();
        Call callpic = findmodel.postpic(Integer.parseInt(postlist.get(position).getPid()));

        callpic.enqueue(new Callback<List<PostPic>>() {
            @Override
            public void onResponse(Call<List<PostPic>> call, Response<List<PostPic>> response) {

                List<PostPic> post = response.body();
                PostPicAdapter postPicAdapter = new PostPicAdapter(context, post);

                GridLayoutManager layoutManager = new GridLayoutManager(context,3);

                viewHolder.rv.setLayoutManager(layoutManager);
                viewHolder.rv.setAdapter(postPicAdapter);
                viewHolder.rv.setItemAnimator(new DefaultItemAnimator());

            }

            @Override
            public void onFailure(Call<List<PostPic>> call, Throwable t) {

            }
        });




        Picasso.with(context)
                .load(MasterImages_URL+postlist.get(position).getUserpic())
                .transform(new RoundTransform(50))
                .into(viewHolder.userpic);

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context,PostActivity.class);
                intent.putExtra("post", post);
                context.startActivity(intent);
            }
        });



    }



    @Override
    public int getItemCount() {
        return postlist.size();
    }
}

